package com.blb.zycommunity.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.SysPost;
import com.blb.zycommunity.entity.SysRole;
import com.blb.zycommunity.mapper.SysRoleMapper;
import com.blb.zycommunity.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public List<SysRole> findAllRoles() {
        return roleMapper.selectAllRoles();
    }

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public void getSysRolePageVo(Page<SysRole> pageRole, String name, String key, String createTime, String status) {
        sysRoleMapper.selectPageVo(pageRole,name,key,createTime,status);
    }

    /**
     * 添加角色和菜单
     * @param sysRole
     * @param MenuId
     */
    @Transactional
    @Override
    public void addSysRole(SysRole sysRole,Object[] MenuId) {
        sysRoleMapper.insert(sysRole);
        for (int i = 0; i < MenuId.length; i++) {
            sysRoleMapper.addInsert(sysRole.getRoleId(),MenuId[i]);
        }
    }


    /**
     * 修改角色
     * @param sysRole
     * @param MenuId
     */
    @Transactional
    @Override
    public void UpdateSysRole(SysRole sysRole, Object[] MenuId) {
        sysRoleMapper.updateById(sysRole);
        sysRoleMapper.deleteById(sysRole.getRoleId());
        for (int i = 0; i < MenuId.length; i++) {
            if (MenuId.length!=0){
                sysRoleMapper.addInsert(sysRole.getRoleId(),MenuId[i]);
            }
        }
    }

    /**
     * 删除信息
     * @param roleId
     */
    @Override
    public void deleteById(Object[]  roleId) {
        for (int i = 0; i < roleId.length; i++) {
            sysRoleMapper.deleteByRoleId(roleId[i]);
        }
    }
}
